package com.chtwm.yfsb.wangyi.controller;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.chtwm.yfsb.wangyi.model.EnvironmentConfig;
import com.chtwm.yfsb.wangyi.util.DynamicDataSourceRegisterUtil;
import com.chtwm.yfsb.wangyi.util.RequestHolder;
import com.chtwm.yfsb.wangyi.util.SessionUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Properties;

@Controller
public class SysController {
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value="submitEnvironmentConfig")
    public String submitEnvironmentConfig(@RequestBody List<EnvironmentConfig> environmentConfigs) throws Exception{
        if(environmentConfigs==null||environmentConfigs.size()==0){
            return "没有数据！";
        }
        //参数校验
        String codes="";
        String names="";
        //生成prop
        Properties properties=new Properties();
        for (EnvironmentConfig e:environmentConfigs){
            if(StringUtils.isEmpty(e.getCode())||
                    StringUtils.isEmpty(e.getName())||
                    StringUtils.isEmpty(e.getDriverClassName())||
                    StringUtils.isEmpty(e.getPassword())||
                    StringUtils.isEmpty(e.getUsername())||
                    StringUtils.isEmpty(e.getUrl())){
                return "所有参数不能为空！";
            }
            codes=codes+","+e.getCode().trim();
            names=names+","+e.getName().trim();
            properties.setProperty(e.getCode().trim()+ DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_SPLIT+DynamicDataSourceRegisterUtil.DRUID_DATASOURCE_PARAM_PREF+DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_PRPO_KEY_DRIVER_CLASS_NAME,e.getDriverClassName());
            properties.setProperty(e.getCode().trim()+DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_SPLIT+DynamicDataSourceRegisterUtil.DRUID_DATASOURCE_PARAM_PREF+ DruidDataSourceFactory.PROP_URL,e.getUrl());
            properties.setProperty(e.getCode().trim()+DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_SPLIT+DynamicDataSourceRegisterUtil.DRUID_DATASOURCE_PARAM_PREF+DruidDataSourceFactory.PROP_USERNAME,e.getUsername());
            properties.setProperty(e.getCode().trim()+DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_SPLIT+DynamicDataSourceRegisterUtil.DRUID_DATASOURCE_PARAM_PREF+DruidDataSourceFactory.PROP_PASSWORD,e.getPassword());
        }
        codes=codes.substring(1);
        names=names.substring(1);
        properties.setProperty(DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_PREF+"keys",codes);
        properties.setProperty(DynamicDataSourceRegisterUtil.DATASOURCE_PARAM_PREF+"names",names);
        DynamicDataSourceRegisterUtil.refreshDataSoureProperties(properties);
        return "添加成功！";
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value="changeEnv")
    public String changeEnv(@RequestParam String env) throws Exception{
        if(StringUtils.isEmpty(env)) env = "dev";
        RequestHolder.setSession(SessionUtil.ENVIRONMENT_CODE,env);
        return "环境切换到："+env;
    }
    
}
